//  课堂记录列表
let classLogList = async (ctx, next) => {
  const { classid } = ctx.request.body;
  let studentList = await ctx.db.querys(`select * from student where classid="${classid}"`)
  let classCaseList = await ctx.db.querys(`select * from classcase where classid="${classid}"`)

  ctx.body = { code: 1, msg: '数据请求成功', data: { studentList, classCaseList } }
}



// 添加课堂记录
let addClassLog = async (ctx, next) => {
  let isOpr = await ctx.isJudge(ctx, 'A');

  // 判断当前用户是否可以操作
  if (!isOpr) {
    ctx.body = { code: 0, msg: '您无权操作' }
    return
  }
  const { classid, time, arr } = ctx.request.body
  let obj = {};
  arr.forEach(item => {
    Object.keys(item).map(jtem => {
      if (!item[jtem]) {
        obj[jtem] = obj[jtem] + ',' + item.id
      }
    })
  })
  let obj1 = {};
  Object.keys(obj).map(item => {
    let a = obj[item].slice(10, obj[item].length)
    obj1[item] = a
  })
  obj1.classid = classid;
  obj1.time = time + '';

  let results = await ctx.db.insert('classcase', obj1)


  if (results.affectedRows === 1) {
    ctx.body = { code: 1, msg: '添加成功' }
    return
  }
  ctx.body = { code: 0, msg: "添加失败" }
}



//  课堂记录选项卡
let searchClassCase = async (ctx, next) => {
  const { time, classid } = ctx.request.body;
  let classCaseList = await ctx.db.querys(`select * from classcase where classid="${classid}"`)

  if (!classCaseList.length) {
    ctx.body = { code: 0, msg: '未找到数据' }
    return
  }
  let arr = classCaseList.filter(item => {
    return new Date(item.time * 1).toLocaleDateString() === new Date(time * 1).toLocaleDateString()
  })
  ctx.body = { code: 1, msg: '数据请求成功', arr }
}



export default {
  classLogList, addClassLog, searchClassCase
}